package q_javadb;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.ScrollPaneConstants;

public class LocalConnection extends JInternalFrame {
	String driver = "org.apache.derby.jdbc.EmbeddedDriver";
//	String driver = "org.apache.derby.jdbc.ClientDriver";
	
	String url = "jdbc:derby:bookrent;create=true;user=bookrent;password=bookrent";
	Connection conn;
	
	
	private JPanel panel;
	private JButton btnConnect;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JButton btnNewButton;


	public LocalConnection() {
		super("Local Connection Test", true, true, true, true);
		setBounds(100, 100, 408, 261);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getBtnConnect());
			panel.add(getBtnNewButton());
		}
		return panel;
	}
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton("connect");
			btnConnect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						Class.forName(driver).newInstance();
						textArea.append("드라이버 로딩 ok.....\n");
						
						conn = DriverManager.getConnection(url);
						textArea.append("Connection 객체가 생성되었습니다.\n");
						
					}catch(Exception ex){
						textArea.append("연결 오류.....\n");
						textArea.append(ex.getMessage());
					}
				}
			});
		}
		return btnConnect;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setWrapStyleWord(true);
		}
		return textArea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("테이블 생성");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					String sql = "create table book(serial integer, mid varchar(30))";
					try{
						DatabaseMetaData dmd = conn.getMetaData();
						ResultSet rs = dmd.getTables(null, null, "book", null);
						
						if(!rs.next()){
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.execute();
							textArea.setText("book 테이블이 생성되었습니다.\n");
						}
					}catch(Exception ex){
						textArea.setText(ex.toString() + "\n");
					}
					
					
				}
			});
		}
		return btnNewButton;
	}
}
